// $Id$
// Licensed under Apache License Version 2.0
// See: http://www.apache.org/licenses/LICENSE-2.0.html
package de.servicezombie.tcmdb.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import de.servicezombie.lang.Configuration;
import de.servicezombie.tcmdb.dao.mongo.MongoUtils;
import de.servicezombie.tcmdb.rest.vo.WsTestExecution;
import de.servicezombie.tcmdb.rest.vo.WsTestcaseDefinition;
import de.servicezombie.tcmdb.util.TestExecutionUtils;

public class TestExecutionManipulationServiceDefaultImpl implements TestExecutionManipulationService {

	private static final Logger log = LoggerFactory.getLogger(TestExecutionManipulationServiceDefaultImpl.class);
	
	private Configuration configuration;

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	public WsTestExecution buildInstanceBy(WsTestcaseDefinition definition) {
		if (log.isDebugEnabled()) {
			log.debug("buildInstanceBy: >> " + definition);
		}
		
		WsTestExecution execution = new WsTestExecution();
		TestExecutionUtils.copyValuesFromDefinition(definition, execution);		
		applyDefaultValues(execution);
		
		if (log.isDebugEnabled()) {
			log.debug("buildInstanceBy: << " + execution);
		}
		return execution;
	}

	private void applyDefaultValues(WsTestExecution execution) {
		execution.setAffectedVersion(null);		
		execution.setExecutionEffort(null);
		execution.setExecutionRemark(null);
		
		execution.setResolution(TestExecutionUtils.getDefaultResolution(configuration));
		execution.setResponsible(null);
		
		MongoUtils.invalidateId(execution);
	}

	@Override
	public void copy(WsTestExecution from, WsTestExecution into, FieldCopyStrategy strategy) {
		
		if (log.isDebugEnabled()) {
			log.debug("copy: >> " + from + ", " + into + ", " + strategy);
		}
		
		strategy.copy(from, into);

		if (log.isDebugEnabled()) {
			log.debug("copy: into = " + into);
			log.debug("copy: <<");
		}
	}
}
